// pages/select_address/select_address.js
const api = require('../../utils/util.js');
const service = require('../../utils/service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addList: [],
        cart_ids: '',
        options: '',
        is_yzc: '',
        address_id: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let _this = this;
        if(options.hasOwnProperty('cart_ids')) {
            this.setData({
                cart_ids: options.cart_ids
            })
        }
        if(options.hasOwnProperty('options')) {
            console.log(JSON.stringify(options));
            if(options.address_id) {
                this.setData({
                    address_id: options.address_id
                })
                console.log(this.data.address_id)
            }

            this.setData({
                options: options.options
            })

        }
        if(options.hasOwnProperty('is_yzc')) {
            this.setData({
                is_yzc: options.is_yzc
            })
        }

    },

    radioChange: function(e) {
        let _this = this;
        wx.navigateBack({
            success: function() {
                if(getCurrentPages().length != 0) {
                    //刷新当前页面的数据
                    let options = {
                        cart_ids: _this.data.cart_ids,
                        options: _this.data.options,
                        address_id: e.detail.value,
                        is_yzc: _this.data.is_yzc,
                    }
                    getCurrentPages()[getCurrentPages().length - 1].onLoad(options)
                }
            }
        })
    },

    toAddress: function() {
        wx.navigateTo({
            url: '../address/address'
        })
    },
    onShow: function(){
    	let _this = this;
    	api.ajax(service.url('getMyAddress'), {}, function(res) {
            console.log(res)
            let addList = res.data;
            _this.setData({
                addList: addList
            })

        })
    }
})